﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SysRouteLinq
{
    public partial class CadLinha : Form
    {
        public CadLinha()
        {
            InitializeComponent();
        }

        private SysRouteDataContext dc = new SysRouteDataContext();

        private void CadLinha_Shown(object sender, EventArgs e)
        {
            IEnumerable<Empresa> r = from f in dc.Empresas
                                     orderby
                                         f.nome
                                     select f;
            comboBox1.DataSource = r.ToList();
            comboBox1.DisplayMember = "nome";
            comboBox1.ValueMember = "id_empresa";

            IEnumerable<Parada> p = from f in dc.Paradas
                                    orderby
                                        f.nome
                                    select f;
            paradasDaLinha.DataSource = p.ToList();
            paradasDaLinha.DisplayMember = "nome";
            paradasDaLinha.ValueMember = "id_parada";

            listBox1.DisplayMember = "nome";
        }


        private void button1_Click(object sender, EventArgs e)
        {
            {
                Linha lin = new Linha
                {
                    numero = textBox1.Text,
                    descricao = textBox2.Text,
                    referencia = textBox3.Text,
                    id_empresa = (int)comboBox1.SelectedValue,
                };
                dc.Linhas.InsertOnSubmit(lin);
                dc.SubmitChanges();

                int x = 0;

                foreach (Parada p in listBox1.Items)
                {
                    LinhaParada lp = new LinhaParada();
                    lp.id_linha = lin.id_linha;
                    lp.id_parada = p.id_parada;
                    lp.ordem = ++x;
                    dc.LinhaParadas.InsertOnSubmit(lp);
                    dc.SubmitChanges();

                }
                
                MessageBox.Show("Cadastro Realizado com sucesso");

                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                listBox1.Items.Clear();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count > 0)
            {
                bool result = false;

                foreach (var item in listBox1.Items)
                {
                    if (item == paradasDaLinha.SelectedItem)
                    {
                        result = false;
                        break;
                    }
                    else
                    {
                        result = true;
                    }
                }

                if (result == true)
                {
                    listBox1.Items.Add(paradasDaLinha.SelectedItem);
                }
                else
                {
                    MessageBox.Show("Item já inserido, selecione outro.");
                }
            }
            else
            {
                listBox1.Items.Add(paradasDaLinha.SelectedItem);
            }
        }
       
    }
}
